#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

#define GPIO "/sys/class/gpio"
#define GPIO_PATH "/sys/class/gpio/gpio37"

void write_to_file(const char *path, const char *value) {
    FILE *file = fopen(path, "w");
    if (file == NULL) {
        perror("Failed to open file");
        exit(EXIT_FAILURE);
    }
    fprintf(file, "%s", value);
    fclose(file);
}

void signal_handler(int arg)
{
    // 取消导出 GPIO 设备
    write_to_file(GPIO "/unexport", "37");

    exit(0);
}

int main() {
    
    signal(SIGINT, signal_handler);

    // 导出 GPIO 设备
    write_to_file(GPIO "/export", "37");

    // 设置为输出模式
    write_to_file(GPIO_PATH "/direction", "out");
    
    while(1){
        // 设置为高电平
        write_to_file(GPIO_PATH "/value", "1");

        sleep(1);

        // 设置为低电平
        write_to_file(GPIO_PATH "/value", "0");

        sleep(1);
    }

    return 0;
}
